package com.tuiyun.jxgl.dao;

// Generated Feb 1, 2013 4:10:46 PM by Hibernate Tools 3.4.0.CR1

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;

import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Driver.
 * @see com.tuiyun.jxgl.Driver
 * @author Hibernate Tools
 */
public class DriverHome extends HibernateDaoSupport{

	private static final Log log = LogFactory.getLog(DriverHome.class);	

	public void persist(Driver transientInstance) {
		log.debug("persisting Driver instance");
		try {
			getHibernateTemplate().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Driver instance) {
		log.debug("attaching dirty Driver instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Driver instance) {
		log.debug("attaching clean Driver instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Driver persistentInstance) {
		log.debug("deleting Driver instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Driver merge(Driver detachedInstance) {
		log.debug("merging Driver instance");
		try {
			Driver result = (Driver) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
		
	
	public Driver findByUsername(java.lang.String username) {
		log.debug("getting Driver instance with username: " + username);
		try {			
			List<Driver> res = this.getHibernateTemplate().findByNamedParam("from Driver d where d.name = :username", "username", username);
			
			Driver instance = null;
			if(res != null && res.size() == 1){
				instance = res.get(0);
			}
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Driver> findByRange(int pagesize,int pageindex, byte region, byte needType){
		log.debug("getting Driver instances with rage_size: " + pagesize + " ,rangeindex: " + pageindex);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Driver d";
			if(region != 0){
				sql += " where d.driverRegion = ?";
				if(needType != -1)
					sql += " and d.driverType = ?";
			}else{
				if(needType != -1)
					sql += " where d.driverType = ?";
			}
			
			sql += " order by d.carCode";
			Query q = s.createQuery(sql);
			if(region != 0){
				q.setByte(0, region);
				if(needType != -1)
					q.setByte(1, needType);
			}else{
				if(needType != -1)
					q.setByte(0, needType);
			}
			
			q.setFirstResult((pageindex-1)*pagesize);
			q.setMaxResults(pagesize);
			List<Driver> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}		
	}
	
	public int findCount(byte region, byte needType){
		log.debug("getting Driver instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct d.id) from com.tuiyun.jxgl.bean.Driver d";
			if(region != 0){
				sql += " where d.driverRegion = ?";
				if(needType != -1)
					sql += " and d.driverType = ?";
			}else{
				if(needType != -1)
					sql += " where d.driverType = ?";
			}
			
			Query q = s.createQuery(sql);
			if(region != 0){
				q.setByte(0, region);
				if(needType != -1)
					q.setByte(1, needType);
			}else{
				if(needType != -1)
					q.setByte(0, needType);
			}
			
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}

	}
	
	public Driver findById(java.lang.Integer id) {
		log.debug("getting Driver instance with id: " + id);
		try {
			Driver instance = (Driver) getHibernateTemplate().get(
					"com.tuiyun.jxgl.bean.Driver", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Driver> findByExample(Driver instance) {
		log.debug("finding Driver instance by example");
		try {
			List<Driver> results = (List<Driver>) getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public int findCountLikeName(String qw, byte region, byte needType) {
		log.debug("getting Driver instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(distinct d.id) from com.tuiyun.jxgl.bean.Driver d where d.realName like ?";
			if(region != 0){
				sql += " and d.driverRegion = ?";
			}
			if(needType != -1){
				sql += " and d.driverType = ?";
			}
			Query q = s.createQuery(sql);			
			q.setString(0, "%"+qw+"%");
			if(region != 0){
				q.setByte(1, region);
				if(needType != -1)
					q.setByte(2, needType);
			}else{
				if(needType != -1)
					q.setByte(1, needType);
			}			
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Driver> findByRangeLikeName(int page_size, int page_index,String qw, byte region, byte needType) {
		log.debug("getting Driver instances with rage_size and query word: " + page_size + " ,rangeindex: " + page_index);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "from com.tuiyun.jxgl.bean.Driver d where d.realName like ?";
			if(region != 0){
				sql += " and d.driverRegion = ?";
			}
			if(needType != -1){
				sql += " and d.driverType = ?";
			}
			sql += " order by d.carCode";
			Query q = s.createQuery(sql);
			q.setString(0, "%"+qw+"%");
			
			if(region != 0){
				q.setByte(1, region);
				if(needType != -1){
					q.setByte(2, needType);
				}
			}else{
				if(needType != -1){
					q.setByte(1, needType);
				}
			}
			
			q.setFirstResult((page_index-1)*page_size);
			q.setMaxResults(page_size);
			List<Driver> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}	}
}
